<template>
	<Header class="header">
    <div class="header-menu">
      <router-link :to="{ name: 'home', params: {} }"><img data-v-f4352c2c="" src="//www.lvjvclub.com/dist/9bec35f4ce986c7c7987abcf59c72adb.png" class="index_logo_icon"></router-link>
      <div style="display: inline-block;margin-left: 20px;cursor: pointer">
        <el-dropdown trigger="click" @command="routeGo">
          <span class="el-dropdown-link">
            {{nowCheck}}<i class="el-icon-arrow-down el-icon--right"></i>
          </span>
          <el-dropdown-menu slot="dropdown">
            <div class="drop-title">集团账号</div>
            <template v-for="m in dropData" v-if="m.type===1">
              <el-dropdown-item :data-auth="m.auth" :class="{'menu-active':nowCheck===m.name}" :command="m">{{m.name}}</el-dropdown-item>
            </template>
            <div class="drop-title">门店账号</div>
            <template v-for="m in dropData" v-if="m.type===3">
              <el-dropdown-item :data-auth="m.auth" :class="{'menu-active':nowCheck===m.name}" :command="m">{{m.name}}</el-dropdown-item>
            </template>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
      <template v-for="m in menus">
				<div class="first-menu">
	        <a href="javascript:void(0);" :data-auth="m.auth" @click="changeMenu(m)" :class="{'router-link-exact-active router-link-active':$store.state.httpconfig.token===m.type}">
						{{m.name}}
					</a>
					<div v-if="m.siblings">
						<a v-for="n in m.siblings" href="javascript:void(0);" :data-auth="n.auth" @click="changeMenu(n)" :class="{'router-link-exact-active router-link-active':$store.state.httpconfig.token===n.type}">
							{{n.name}}
						</a>
					</div>
				</div>
			</template>
    </div>
		<div class="fr">
			您好，<span>
        	{{username}}
	        </span>
	        <a @click="loginOut" style="margin:0 20px;cursor:pointer;">退出登录</a>
		</div>
	</Header>
</template>
<script>
/**
 * @description cookie operation -- get
 * @param key {String} key
 * @returns {String} value
 */
function getCookie (key) {
		var aCookie = document.cookie.split('; ')
		for (var i = 0; i < aCookie.length; i++) {
				var aCrumb = aCookie[i].split('=')
				if (key === aCrumb[0]) {
						return unescape(aCrumb[1])
				}
		}
		return null
}

/**
 * @description cookie operation -- create or set
 * @param key {String} key
 * @param value {String} value
 * @param expire {Number} expire date
 */
function setCookie (key, value, expire) {
		var data = new Date()

		data.setDate(data.getDate() + expire)
		document.cookie = key + '=' + escape(value) +
				((expire === null) ? '' : ';expires=' + data.toGMTString())
}
export default {
        data () {
            return {
							userList:JSON.parse(sessionStorage.getItem('userList')),
              activeIndex: '1',
              username:JSON.parse(sessionStorage.getItem('userList')).UserName,
              nowCheck:'',
              dropData:[],
              menus:[]
            }
        },
        created(){
					_.map(this.userList.Hotels,(val)=>{
						if(val.Type===1){
							this.dropData.push({
								name:val.Name,
								type:val.Type,
								HotelType:val.HotelType,
								Id:val.Id,
								childrens:[
		              {name:"集团订单管理",auth:"bloc_order_management",type:"group-order","url":"group-reserve","icon":"person-stalker","childs":[
		                  {name:"集团预定",auth:"bloc_inventory_view","url":"group-reserve","icon":"person-stalker","childs":[]},
		                  {name:"集团订单",auth:"bloc_order_view","url":"group-orderlists","icon":"person-stalker","childs":[]},
		                  {name:"全部订单",auth:"bloc_allorder_view","url":"group-lists","icon":"person-stalker","childs":[]},
										]
		              },
		              {name:"集团客户管理",auth:"bloc_member_management",type:"group-user","url":"user-lists","icon":"person-stalker","childs":[
		                  {name:"客户列表",auth:"bloc_member_view","url":"user-lists","icon":"person-stalker","childs":[]},
		                  // {name:"会员列表","url":"vip-lists","icon":"person-stalker","childs":[]},
		                  // {name:"会员卡列表","url":"vip-cards","icon":"person-stalker","childs":[]},
		                ]
		              },
		              {name:"集团设置",auth:"bloc_setting_management",type:"group-manage","url":"group-info","icon":"person","childs":[
		                  {name:"集团信息维护",auth:"bloc_info_view","url":"group-info","icon":"person-stalker","childs":[]},
		                  {name:"门店信息维护",auth:"bloc_store_view","url":"group-store","icon":"person-stalker","childs":[]},
		                  {name:"员工管理",auth:"bloc_user_view","url":"person-manage","icon":"person-stalker","childs":[]},
		                  {name:"角色管理",auth:"bloc_role_view","url":"code-manage","icon":"person-stalker","childs":[]},
		                  // {name:"员工管理","url":"person-manage","icon":"person-stalker","childs":[]},
		                ]
		              }
		            ]
							})
						}else{
							this.dropData.push({
								name:val.Name,
								type:val.Type,
								HotelType:val.HotelType,
								Id:val.Id,
								childrens:[
		              {name:"房态管理",auth:"store_roomstatus_management",type:"mincalendar-room","url":"mincalendar-room","icon":"person","childs":[],siblings:[
										{name:"缩略房态",auth:"store_roomstatus_simple",type:"mincalendar-room","url":"mincalendar-room","icon":"person","childs":[]},
										{name:"实时房态",auth:"store_roomstatus_actual",type:"nowcalendar-room","url":"nowcalendar-room","icon":"person","childs":[]},
										{name:"远期房态",auth:"store_roomstatus_actual_query",type:"calendar-room","url":"calendar-room","icon":"person","childs":[]},
									]},
		              {name:"订单管理",auth:"store_order_management",type:"order-manage","url":"store-order","icon":"person-stalker","childs":[
		                  {name:"订单列表",auth:"store_order_view","url":"store-order","icon":"person-stalker","childs":[]},
		                ]
		              },
									{name:"报表",auth:"store_report_management",type:"excels","url":"revenue-excel","icon":"person","childs":[
										{name:"营收报表",auth:"store_report_revenue_view","url":"revenue-excel","icon":"person-stalker","childs":[]},
										{name:"房型出租率统计",auth:"store_report_inventory","url":"roomtype-rental","icon":"person-stalker","childs":[]},
									]},
		              {name:"客户管理",auth:"store_member_management",type:"person-manage","url":"store-vip","icon":"person-stalker","childs":[
		                  {name:"客户列表",auth:"store_member_view","url":"store-vip","icon":"person-stalker","childs":[]},
		                ]
		              },
		              {name:"资源管理",auth:"store_resource_management",type:"store-manage","url":"store-room","icon":"person-stalker","childs":[
		                  {name:"房型设置",auth:"store_category_view","url":"store-room","icon":"person-stalker","childs":[]},
		                  {name:"价格管理",auth:"store_price_view","url":"store-price","icon":"person-stalker","childs":[]},
		                  {name:"消费管理",auth:"store_goods_view","url":"store-info","icon":"person-stalker","childs":[]},
		                  {name:"政策管理",auth:"store_policy_view","url":"policy-info","icon":"person-stalker","childs":[]},
		                  {name:"楼号管理",auth:"store_zone_view","url":"zone-info","icon":"person-stalker","childs":[]},
		                  {name:"旅行社管理",auth:"store_travel_agency_view","url":"hotel-info","icon":"person-stalker","childs":[]},
		                ]
		              },
		            ]
							})
						}
					})
					let data={}
					if(!sessionStorage.getItem('nowmenu')){
						data=this.dropData[0];
						sessionStorage.setItem('nowmenu',JSON.stringify(data))
						this.routeGo(this.dropData[0]);
					}else{
					 	data=JSON.parse(sessionStorage.getItem('nowmenu'));
						this.$ajax.changehotel({type:data.type,id:data.Id}).then(res=>{
			        if(res.Success){
								this.nowCheck=data.name;
								this.menus=data.childrens;
								sessionStorage.setItem('AuthMapper',JSON.stringify(res.Data.AuthList));
								AuthKit.config({
										systemId:0,
										userToken: '',
										apiURL: ''
								})
								AuthKit.on();
			        }
			      })
					}
        },
        methods:{
          routeGo(data){
						this.$ajax.changehotel({type:data.type,id:data.Id}).then(res=>{
			        if(res.Success){
								console.log(data);
								let menu={
									name:data.name,
									type:data.type,
									HotelType:data.HotelType,
									Id:data.Id,
									childrens:[],
								};
								data.childrens.forEach(item=>{
									if(res.Data.AuthList.indexOf(item.auth)>=0){
										if(item.childs.length>0){
											let childMenu=[];
											item.childs.forEach(citem=>{
												if(res.Data.AuthList.indexOf(citem.auth)>=0){
													childMenu.push(citem);
												}
											})
											item.childs=childMenu;
											item.url=item.childs[0].url;
										}
										menu.childrens.push(item);
									}
								})
		            sessionStorage.setItem('nowmenu',JSON.stringify(menu));
		            this.nowCheck=menu.name;
		            this.menus=menu.childrens;
		            this.changeMenu(menu.childrens[0]);
								sessionStorage.setItem('AuthMapper',JSON.stringify(res.Data.AuthList));
								AuthKit.config({
										systemId:0,
										userToken: '',
										apiURL: ''
								})
								AuthKit.on();
			        }
			      })
          },
        	loginOut(){
						this.$ajax.loginOut().then(res=>{
							sessionStorage.clear();
							AuthKit.off()
							this.$router.push({'name':'login'});
						})
        	},
          changeMenu(data){
            this.$store.commit('SET_MENU',data.childs);
						if(data.url==='calendar-room'){
							this.$router.push({
								name:data.url, // 获取当前连接，重新跳转
								query:{
									_time:new Date().getTime()/1000  // 时间戳，刷新当前router
								 }
							})
						}else{
							this.$router.push({name:data.url});
						}
          }
        }
	  }
</script>
